package com.fw.system.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.system.admin.domain.TbAnswerDetail;
import com.fw.system.admin.mapper.TbAnswerDetailMapper;
import com.fw.system.admin.service.ITbAnswerDetailService;
import com.fw.utils.DateUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.fw.common.IdXD;
import com.fw.system.admin.mapper.TbAnswerMapper;
import com.fw.system.admin.domain.TbAnswer;
import com.fw.system.admin.service.ITbAnswerService;
import com.fw.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 题库Service业务层处理
 * 
 * @author yanwei
 * @date 2023-04-04
 */
@Service
public class TbAnswerServiceImpl extends ServiceImpl<TbAnswerMapper, TbAnswer> implements ITbAnswerService
{
    @Autowired
    private TbAnswerMapper tbAnswerMapper;
    
    @Autowired
    private ITbAnswerDetailService tbAnswerDetailService;

    @Autowired
    private IdXD idXD;

    /**
     * 查询题库
     * 
     * @param id 题库ID
     * @return 题库
     */
    @Override
    public TbAnswer selectTbAnswerById(String id)
    {
        TbAnswer tbAnswer = tbAnswerMapper.selectTbAnswerById(id);
        tbAnswer.setTbAnswerDetailList(tbAnswerDetailService.list(Wrappers.<TbAnswerDetail>lambdaQuery().eq(TbAnswerDetail::getAnswerId,id)));
        return tbAnswer;
    }

    /**
     * 查询题库列表
     * 
     * @param tbAnswer 题库
     * @return 题库
     */
    @Override
    public List<TbAnswer> selectTbAnswerList(TbAnswer tbAnswer)
    {
        return tbAnswerMapper.selectTbAnswerList(tbAnswer);
    }

    /**
     * 新增题库
     * 
     * @param tbAnswer 题库
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTbAnswer(TbAnswer tbAnswer)
    {
        String tbAnswerId = idXD.nextId();
        tbAnswer.setCreateTime(DateUtils.getNowDate());
        tbAnswer.setId(tbAnswerId);
        saveOrUpdate(tbAnswer, tbAnswerId);
        return tbAnswerMapper.insertTbAnswer(tbAnswer);
    }

    private void saveOrUpdate(TbAnswer tbAnswer, String tbAnswerId) {

        tbAnswerDetailService.remove(Wrappers.<TbAnswerDetail>lambdaQuery().eq(TbAnswerDetail::getAnswerId,tbAnswerId));
        List<TbAnswerDetail> tbAnswerDetailList = tbAnswer.getTbAnswerDetailList();
        // 看看对不对
        Assert.isTrue(tbAnswerDetailList.size() == tbAnswerDetailList.parallelStream().map(TbAnswerDetail::getSelectName).collect(Collectors.toSet()).size(),"选项名称不可重复!");
        Long answerType = tbAnswer.getAnswerType();
        long count = tbAnswerDetailList.parallelStream().filter(item -> Integer.valueOf(1).equals(item.getIsExactness())).count();
        Assert.isTrue(count >=1,"必须有一个正确答案!");
        if (Long.valueOf(1).equals(answerType)){
            // 如果是单选，那么只能有一个正确答案
            Assert.isTrue(count ==1,"单选,必须有一个正确答案!");

        }
        ArrayList<TbAnswerDetail> tbAnswerDetails = Lists.newArrayList();
        for (TbAnswerDetail tbAnswerDetail : tbAnswerDetailList) {
            tbAnswerDetail.setAnswerId(tbAnswerId);
            tbAnswerDetail.setId(idXD.nextId());
            tbAnswerDetails.add(tbAnswerDetail);
        }
        tbAnswerDetailService.saveBatch(tbAnswerDetails);
    }

    /**
     * 修改题库
     * 
     * @param tbAnswer 题库
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTbAnswer(TbAnswer tbAnswer)
    {        saveOrUpdate(tbAnswer, tbAnswer.getId());
        return tbAnswerMapper.updateTbAnswer(tbAnswer);
    }

    /**
     * 批量删除题库
     * 
     * @param ids 需要删除的题库ID
     * @return 结果
     */
    @Override
    public int deleteTbAnswerByIds(String[] ids)
    {
        tbAnswerDetailService.remove(Wrappers.<TbAnswerDetail>lambdaQuery().in(TbAnswerDetail::getAnswerId,ids));

        return tbAnswerMapper.deleteTbAnswerByIds(ids);
    }

    /**
     * 删除题库信息
     * 
     * @param id 题库ID
     * @return 结果
     */
    @Override
    public int deleteTbAnswerById(String id)
    {
        tbAnswerDetailService.remove(Wrappers.<TbAnswerDetail>lambdaQuery().eq(TbAnswerDetail::getAnswerId,id));
        return tbAnswerMapper.deleteTbAnswerById(id);
    }
}
